
//
//  HomeFunctionCell.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/10/26.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "HomeFunctionCell.h"
#import "SubjectRecommendModel.h"
#import "UIButton+WebCache.h"
@interface HomeFunctionCell()
@property(nonatomic,strong) UIScrollView *scrollView;
@property(nonatomic,strong) MCButton *mcBtn;
@end
static NSString *identifier = @"HomeFunctionCell";
@implementation HomeFunctionCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
+(instancetype)cellClassWithTableView:(UITableView *)tableView
{
    HomeFunctionCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[HomeFunctionCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
//        self.contentView.backgroundColor = kRedColor;
//        [self setUpUI];
//        [self setUpData];
    }
    return self;
}
-(void)setSubjectRecommendArray:(NSArray *)subjectRecommendArray
{
    [self.scrollView removeFromSuperview];
    _subjectRecommendArray = subjectRecommendArray;
    XLog(@"subjectRecommendArray:%@",subjectRecommendArray);
    [self setUpUI];
    [self setUpData];
}
-(void)setUpUI
{
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kUIScreen.size.width, 100)];
    [self.contentView addSubview:self.scrollView];
    self.scrollView.showsHorizontalScrollIndicator = NO;
    CGFloat w = kWidth/4.5;
//    CGFloat margin = 20;
    for (int i = 0; i < self.subjectRecommendArray.count; i++) {
        //margin*(i+1)
        self.mcBtn = [[MCButton alloc]initWithFrame:CGRectMake(w*i, 20, w, 80)];
        self.mcBtn.tag = i;
        [self.scrollView addSubview:self.mcBtn];
        SubjectRecommendModel *model = self.subjectRecommendArray[i];
//        self.mcBtn.backgroundColor = [UIColor orangeColor];
        self.mcBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [self.mcBtn setTitle:model.title forState:UIControlStateNormal];
        [self.mcBtn setTitleColor:kLabel16Color forState:UIControlStateNormal];
        [self.mcBtn sd_setImageWithURL:[NSURL URLWithString:model.icon] forState:UIControlStateNormal];
        [self.mcBtn addTarget:self action:@selector(clickMCBtn:) forControlEvents:UIControlEventTouchUpInside];
        self.mcBtn.buttonStyle = imageTop;
    }
    self.scrollView.contentSize = CGSizeMake((w)*self.subjectRecommendArray.count, 0);
}
-(void)setUpData
{
    
}
-(void)clickMCBtn:(MCButton *)clickMCBtn
{
    if ([self.delegate respondsToSelector:@selector(HomeFunctionCell:withTag:withTitle:)]) {
        [self.delegate HomeFunctionCell:self withTag:clickMCBtn.tag withTitle:[clickMCBtn currentTitle]];
    }
}
@end
